﻿using Ace.Application;
using Ace.Data;
using Ace.Entity.Wiki;
using Ace.Exceptions;
using Ace.IdStrategy;
using Chloe;
using Chloe.MySql;
using Chloe.SqlServer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ace.Application.Wiki
{
    public interface IVideoService : IAppService
    {
        List<Video> GetList(int SortID = 0, int ST = 0, string keyword = "");

        

        void Add(AddVideoInput input);
        void Update(UpdateVideoInput input);
        void Delete(string id );

        Video GetModel(string Id);

        Video GetVideoInfo(string Id);

        PagedData<Video> GetPageData(Pagination page, int SortID=0, int ST=-1, string keyword="");

        PagedData<VideoItem> GetVideoItemPageData(Pagination page, int SortID=0, int ST = -1, string keyword = "");


        PagedData<VideoInfo> GetVideoInfoPageData(Pagination page, int OrderType, int SortID=0, int ST = -1, string keyword = "",int SortID2=0);
    }

    public class VideoService : AppServiceBase<Video>, IVideoService
    {
        public VideoService(IDbContext dbContext, IServiceProvider services) : base(dbContext, services)
        {
        }

        public List<Video> GetList(int SortID=0, int ST=0, string keyword = "")
        {
            var q = this.Query;
 

            if (ST > -1)
            {
                q = q.Where(a => a.ST == ST);
            }

            if (SortID >0)
            {
                q = q.Where(a => a.SortID == SortID);
            }

            if (keyword.IsNotNullOrEmpty())
            {
                q = q.Where(a => a.Title.Contains(keyword));
            }

            var ret = q.ToList();
            return ret;
        }

        

        public void Add(AddVideoInput input)
        {
            this.InsertFromDto(input);
        }
        public void Update(UpdateVideoInput input)
        {
            this.UpdateFromDto(input);
        }

        public Video GetModel(string Id)
        {
            return  this.Query.Where(a => a.Id == Id).FirstOrDefault();
        }

        public void Delete(string id)
        {
            this.DbContext.Delete<Video>(a => a.Id == id);
        }


        public PagedData<Video> GetPageData(Pagination page, int SortID = 0, int ST =0, string keyword="")
        {
            var q = this.DbContext.Query<Video>();

            q = q.WhereIfNotNullOrEmpty(keyword, a => a.Title.Contains(keyword) );

            if (SortID > 0)
            {
                q = q.Where(a => a.SortID == SortID);
            }

            if (ST > -1)
            {
                q = q.Where(a => a.ST == ST);
            }

            PagedData<Video> pagedData = q.TakePageData(page);
            
            return pagedData;
        }



        public Video GetVideoInfo(string Id)
        {
            string sql = " select * from video where Id=?Id ";

            Video info = this.DbContext.SqlQuery<Video>(sql, new { Id = Id }).FirstOrDefault();
            return info;
        }



        public PagedData<VideoItem> GetVideoItemPageData(Pagination page, int SortID=0, int ST=0, string keyword = "")
        {
            string strFileds = " Id,Title,ImageUrl,Remark,FileUrl ";

            string strWhere = " 1=1 ";

            
             
            if (ST > -1)
            {
                strWhere += " and ST=" + ST;
            }

            if (SortID >0)
            {
                strWhere += " and SortID=" + SortID;
            }

            if (!string.IsNullOrEmpty(keyword))
            {
                strWhere += " and (Title like '%" + keyword + "%' )";
            }


            DbParam _totalcount = new DbParam("?_totalcount", null, typeof(int)) { Direction = ParamDirection.Output };
            DbParam _pagecount = new DbParam("?_pagecount", null, typeof(int)) { Direction = ParamDirection.Output };

            DbParam[] dbs = new DbParam[] {
                new DbParam("?_fields", strFileds),
                new DbParam("?_tables", " video "),
                new DbParam("?_where",strWhere),
                new DbParam("?_orderby", "CreateDate desc"),
                new DbParam("?_pageindex", page.Page),
                new DbParam("?_pageSize", page.PageSize),
                _totalcount,
                _pagecount
        };

            List<VideoItem> list = this.DbContext.SqlQuery<VideoItem>("sp_viewPage", System.Data.CommandType.StoredProcedure, dbs).ToList();


            PagedData<VideoItem> pagedData = new PagedData<VideoItem>();
            pagedData.CurrentPage = page.Page;
            pagedData.Models = list;
            pagedData.PageSize = page.PageSize;
            pagedData.TotalCount = Convert.ToInt32(_totalcount.Value);
            pagedData.TotalPage = Convert.ToInt32(_pagecount.Value);

            return pagedData;
        }

        public PagedData<VideoInfo> GetVideoInfoPageData(Pagination page,int OrderType, int SortID=0, int ST = 0, string keyword = "",int SortID2=0)
        {
            string strFileds = " *,f_video_log_count(Id) as Count ";

            string strWhere = " 1=1 ";

            if (SortID > 0)
            {
                strWhere += " and SortID=" + SortID;
            }

            if (SortID2 > 0)
            {
                strWhere += " and SortID2=" + SortID2;
            }

            if (ST > -1)
            {
                strWhere += " and ST=" + ST;
            }

            if (!string.IsNullOrEmpty(keyword))
            {
                strWhere += " and Title like '%" + keyword + "%' ";
            }

            string OrderBy = "";
            if(OrderType==0)
            {                
                OrderBy = "Ord asc";
            }
            else
            {
                OrderBy = "CreateDate desc";
            }

            DbParam _totalcount = new DbParam("?_totalcount", null, typeof(int)) { Direction = ParamDirection.Output };
            DbParam _pagecount = new DbParam("?_pagecount", null, typeof(int)) { Direction = ParamDirection.Output };

            DbParam[] dbs = new DbParam[] {
                new DbParam("?_fields", strFileds),
                new DbParam("?_tables", " video "),
                new DbParam("?_where",strWhere),
                new DbParam("?_orderby", OrderBy),
                new DbParam("?_pageindex", page.Page),
                new DbParam("?_pageSize", page.PageSize),
                _totalcount,
                _pagecount
        };

            List<VideoInfo> list = this.DbContext.SqlQuery<VideoInfo>("sp_viewPage", System.Data.CommandType.StoredProcedure, dbs).ToList();


            PagedData<VideoInfo> pagedData = new PagedData<VideoInfo>();
            pagedData.CurrentPage = page.Page;
            pagedData.Models = list;
            pagedData.PageSize = page.PageSize;
            pagedData.TotalCount = Convert.ToInt32(_totalcount.Value);
            pagedData.TotalPage = Convert.ToInt32(_pagecount.Value);

            return pagedData;
        }

    }

}
